package com.plter.controls.plter
{
	
	import flash.display.DisplayObject;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class PTwoSidesPanel extends PUIComponent
	{
		private var _aSide:PSprite;
		private var _bSide:PSprite;
		private var _aSideContainer:PSprite;
		private var _bSideContainer:PSprite;

		private var _timer:Timer;

		private var _directionOfRotation:String;

		private var _speed:int=10;

		public function PTwoSidesPanel()
		{
			super();

			_timer=new Timer(20);
			_timer.addEventListener(TimerEvent.TIMER, timerHandler);

			_aSide=new PSprite;
			_bSide=new PSprite;
			_aSideContainer=new PSprite;
			_bSideContainer=new PSprite;
			_bSideContainer.rotationY=180;
			super.addChild(_bSideContainer);
			super.addChild(_aSideContainer);
			_aSideContainer.addChild(_aSide);
			_bSideContainer.addChild(_bSide);
			_bSideContainer.visible=false;

			this.width=400;
			this.height=300;
			directionOfRotation == PTwoSidesPanelDirectionOfRotation.FORWARD_DIRECTION;
		}

		/**
		 * 取得旋转方向
		 */
		public function get directionOfRotation():String
		{
			return _directionOfRotation;
		}

		/**
		 * 设置旋转方向
		 */
		public function set directionOfRotation(value:String):void
		{
			_directionOfRotation=value;

			switch (_directionOfRotation)
			{
				case PTwoSidesPanelDirectionOfRotation.FORWARD_DIRECTION:
					_speed=Math.abs(_speed);
					break;
				case PTwoSidesPanelDirectionOfRotation.BACKWARD_DIRECTION:
					_speed=-Math.abs(_speed);
					break;
			}
		}


		/**
		 * 取得双面容器的a面
		 */
		public function get aSide():PSprite
		{
			return _aSide;
		}

		/**
		 * 取得双面容器的b面
		 */
		public function get bSide():PSprite
		{
			return _bSide;
		}

		private var _width:Number=0;

		override public function get width():Number
		{
			return _width;
		}

		override public function set width(value:Number):void
		{
			_width=value;
			_aSideContainer.x=_width / 2;
			_bSideContainer.x=_width / 2;
			_aSide.x=-_width / 2;
			_bSide.x=-_width / 2;
		}

		private var _height:Number=0;

		override public function get height():Number
		{
			return _height;
		}

		override public function set height(value:Number):void
		{
			_height=value;
		}

		/**
		 * 把面板反过来
		 */
		public function turnOver(direction:String="forwardDirection"):void
		{
			directionOfRotation=direction;
			startTurnOver();
		}

		override public function addChild(child:DisplayObject):DisplayObject
		{
			throw new Error("No such method");
			return null;
		}

		override public function addChildAt(child:DisplayObject, index:int):DisplayObject
		{
			throw new Error("No such method");
			return null;
		}

		override public function contains(child:DisplayObject):Boolean
		{
			throw new Error("No such method");
			return false;
		}

		override public function getChildAt(index:int):DisplayObject
		{
			throw new Error("No such method");
			return false;
		}

		override public function getChildByName(name:String):DisplayObject
		{
			throw new Error("No such method");
			return null;
		}

		override public function getChildIndex(child:DisplayObject):int
		{
			throw new Error("No such method");
			return -1;
		}

		override public function removeChild(child:DisplayObject):DisplayObject
		{
			throw new Error("No such method");
			return null;
		}

		override public function removeChildAt(index:int):DisplayObject
		{
			throw new Error("No such method");
			return null;
		}

		private function startTurnOver():void
		{
			if (!_timer.running)
			{
				_timer.start();
			}
		}

		private function timerHandler(event:TimerEvent):void
		{
			_aSideContainer.rotationY+=_speed;
			_bSideContainer.rotationY+=_speed;

			if ((_aSideContainer.rotationY % 360 > 90 && _aSideContainer.rotationY % 360 < 270) || (_aSideContainer.rotationY % 360 < -90 && _aSideContainer.rotationY % 360 > -270))
			{
				super.setChildIndex(_aSideContainer, 0);
				_aSideContainer.visible=false;
				_bSideContainer.visible=true;
			}
			else
			{
				super.setChildIndex(_bSideContainer, 0);
				_aSideContainer.visible=true;
				_bSideContainer.visible=false;
			}

			if (_aSideContainer.rotationY % 180 == 0 && _bSideContainer.rotationY % 180 == 0)
			{
				_timer.stop();
			}

			event.updateAfterEvent();
		}
	}
}